package current.security.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5 {
	public static String str;
	//plainText需要加密的字符串，yz盐值
	public static String md5s(String plainText,String yz) {
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			plainText = MD5.getString(plainText+yz);
			System.out.println("加密字串："+plainText);
			md.update(plainText.getBytes());
			byte b[] = md.digest();
			int i;
			StringBuffer buf = new StringBuffer();
			for (int offset = 0; offset < b.length; offset++) {
				i = b[offset];
				if (i < 0){
					i += 256;
				}
				if (i < 16){
					buf.append("0");
				}
				buf.append(Integer.toHexString(i));
			}
			str = buf.toString();
			System.out.println("加密结果: " + buf.toString());// 32位的加密
			return str;
//			System.out.println("result: " + buf.toString().substring(8, 24));// 16位的加密
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	private static String getString(String str){
		char []c = str.toCharArray();
		int i = str.length();
		int k = (i/2*i/3-4/5*6)%2;
		StringBuffer s = new StringBuffer();
		for(int j = 0 ; j < c.length ; j ++ ){
			if(j%2==0){
				s.append(k);
			}else{
				s.append(i/2);
			}if(j%3==1){
				s.append("8");
			}else{
				s.append("6");
			}
			s.append(c[j]);
		}
		return s.toString();
	}

	public static void main(String agrs[]) {
		MD5.md5s("qyjl","qyjl");// 加密4
	}

}